package com.yuexunit.operationman.nbsy.module.Login.ui;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;


import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.Login.adapter.SearchListAdapter;
import com.yuexunit.operationman.nbsy.module.Login.bean.PatientBean;
import com.yuexunit.operationman.nbsy.module.Patients.ui.Act_PatientDetail;
import com.yuexunit.operationman.nbsy.widget.EmptyLayout;
import com.yuexunit.operationman.nbsy.widget.FooterLoadingLayout;
import com.yuexunit.operationman.nbsy.widget.PullToRefreshBase;
import com.yuexunit.operationman.nbsy.widget.PullToRefreshList;

import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/2/28 0028.
 */

public class Act_SearchList extends BaseKJActivity {
    @Bind(R.id.patient_list)
    PullToRefreshList patientList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;
    @Bind(R.id.titlebar_tv_title)
    TextView titlebarTvTitle;

    private ListView mList;
    BaseAdapter adapter;

    ArrayList<PatientBean.ObjectEntity> objectEntities = new ArrayList<>();


    @Override
    public void setRootView() {
        setContentView(R.layout.act_patientlist);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        setTitle();
        listViewPreference();
        objectEntities = (ArrayList<PatientBean.ObjectEntity>) getIntent().getExtras().get("object");

        adapter = new SearchListAdapter(mList,objectEntities,R.layout.item_search);
        mList.setAdapter(adapter);
        emptyLayout.dismiss();

        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PreferenceHelper.write(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_NAME, objectEntities.get(position).getPatientName());
                PreferenceHelper.write(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_AGE, objectEntities.get(position).getPatientAge()+"");
                PreferenceHelper.write(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_SEX, objectEntities.get(position).getPatientSex());
                PreferenceHelper.write(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_DATE, objectEntities.get(position).getHospitalDateStr());
                PreferenceHelper.write(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_DOCTOR, objectEntities.get(position).getOperator());
                PreferenceHelper.write(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_BED, objectEntities.get(position).getPatientBed());
                PreferenceHelper.write(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_DIAGNOSIS, objectEntities.get(position).getPreOperation());
                PreferenceHelper.write(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_VISITNO, objectEntities.get(position).getInHospitalNo());
                PreferenceHelper.write(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_OPERATION_ID, objectEntities.get(position).getOperationNo()+"");
                PreferenceHelper.write(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_DISEASE, objectEntities.get(position).getOperandi());
                PreferenceHelper.write(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_ORDER_TIME, objectEntities.get(position).getOperationOrderDateStr());
                AppConfig.initDate();
//                if(objectEntities.get(position).getDepartment().contains("眼"))
//                {
//                    AppConfig.isEye = true;
//                }
//                else {
//                    AppConfig.isEye = false;
//                }
                startActivity(new Intent(aty, Act_PatientDetail.class));
            }
        });
    }

    private void setTitle()
    {
        titlebarTvTitle.setText("手术列表");
    }

    /**
     * 初始化ListView样式
     */
    private void listViewPreference() {
        mList = patientList.getRefreshView();
        mList.setDivider(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        mList.setOverscrollFooter(null);
        mList.setOverscrollHeader(null);
        mList.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        patientList.setPullLoadEnabled(false);//设置上拉刷新功能关闭
        ((FooterLoadingLayout) patientList.getFooterLoadingLayout())
                .setNoMoreDataText("已经是最新状态");

        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
            }
        });

        patientList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
//                AppConfig.ISOVER_PATIENT = false;
//                changeClass();
            }

            @Override
            public void onPullUpToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
//                mRefreshLayout.setHasMoreData(false);
            }
        });

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);

            }
        });
    }

    @OnClick(R.id.titlebar_img_back)
    public void onViewClicked() {
        finish();
    }
}
